/*
Copyright (C) 2008 raohmaru <http://www.raohmaru.com>

This file is part of Raohmaru AS3 Framework.

Raohmaru AS3 Framework is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Raohmaru AS3 Framework is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with Raohmaru AS3 Framework.  If not, see <http://www.gnu.org/licenses/>.
*/

package jp.raohmaru.events
{
import flash.display.*;
import flash.events.*;

/**
 * Registrador de múltiples eventos comunes a objetos interactivos.
 * 
 * @author raohmaru
 * @version 1.1.2
 */
public class EventRegister
{
	/**
	* Añade eventos y comportamientos de botón a un objeto. Por defecto añade los eventos del grupo <code>EventGroup.MOUSE_EVENTS</code>.
	* @see EventDispatcher#addEventListener()
	* @param target El objeto que recibirá los eventos
	* @param callback Función del detector que procesa el evento
	* @param flags (= <code>EventGroup.MOUSE_EVENTS</code>) — Conjunto de los eventos que se registrarán (unidos por el operador binario |)
	* @param buttonMode Boolean = true
	* @param mouseChildren Boolean = false
	* @param useCapture : Boolean = false
	* @param priority : int = 0
	* @param useWeakReference : Boolean = false
	 * @example
	<listing version="3.0">EventRegister.addEventsListener(boton, eventHandler);<br>
EventRegister.addEventsListener(boton, eventHandler, EventRegister.ROLL_EVENTS);</br>
EventRegister.addEventsListener(boton, eventHandler, EventRegister.MOUSE_OVER | EventRegister.CLICK);</listing>
	*/
	public static function addEventsListener(	target : InteractiveObject, callback : Function, flags : uint = 11, // 11 -> EventGroup.MOUSE_EVENTS
												buttonMode : Boolean = true, mouseChildren : Boolean = false,
												useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void
	{
		registerEvents(true, target, callback, flags, buttonMode, mouseChildren, useCapture, priority, useWeakReference);
	}

	/**
	* Quita eventos registrados en un objeto y los comportamientos de botón. Por defecto quita los eventos del grupo <code>EventGroup.MOUSE_EVENTS</code>.
	* @see EventDispatcher#removeEventListener()
	* @param target El objeto del que se elminarán los eventos
	* @param callback Función del detector que procesa el evento
	* @param flags (= <code>EventGroup.MOUSE_EVENTS</code>) —  Los tipos de eventos que se quitarán (unidos por el operador binario |)
	* @param buttonMode Boolean = false
	* @param mouseChildren Boolean = true
	* @param useCapture : Boolean = false
	 * @example
	<listing version="3.0">EventRegister.removeEventsListener(boton, eventHandler);<br>
EventRegister.removeEventsListener(boton, eventHandler, EventRegister.ROLL_EVENTS);</br>
EventRegister.removeEventsListener(boton, eventHandler, EventRegister.MOUSE_OVER | EventRegister.CLICK);</listing>
	*/
	public static function removeEventsListener(target : InteractiveObject, callback : Function, flags : uint = 11, // 11 -> EventGroup.MOUSE_EVENTS
												buttonMode : Boolean = false, mouseChildren : Boolean = true,
												useCapture : Boolean = false) : void
	{
		registerEvents(false, target, callback, flags, buttonMode, mouseChildren, useCapture);
	}
  
	/**
	 * Extrae los eventos por su valor binario.
	 * @param flags Conjunto de los eventos que se registrarán (unidos por el operador binario |)
	 */
	private static function extractEventsFromFlags(flags : uint) : Array
	{
		var events : Array = [];
		
		if((flags & EventType.MOUSE_OVER)	> 0) events.push(MouseEvent.MOUSE_OVER);
		if((flags & EventType.MOUSE_OUT)	> 0) events.push(MouseEvent.MOUSE_OUT);
		if((flags & EventType.MOUSE_DOWN)	> 0) events.push(MouseEvent.MOUSE_DOWN);
		if((flags & EventType.MOUSE_UP)		> 0) events.push(MouseEvent.MOUSE_UP);
		if((flags & EventType.CLICK)		> 0) events.push(MouseEvent.CLICK);
		if((flags & EventType.DOUBLE_CLICK)	> 0) events.push(MouseEvent.DOUBLE_CLICK);
		if((flags & EventType.ROLL_OVER)	> 0) events.push(MouseEvent.ROLL_OVER);
		if((flags & EventType.ROLL_OUT)		> 0) events.push(MouseEvent.ROLL_OUT);
		if((flags & EventType.MOUSE_MOVE)	> 0) events.push(MouseEvent.MOUSE_MOVE);
		if((flags & EventType.MOUSE_WHEEL)	> 0) events.push(MouseEvent.MOUSE_WHEEL);
		if((flags & EventType.FOCUS_IN)		> 0) events.push(FocusEvent.FOCUS_IN);
		if((flags & EventType.FOCUS_OUT)	> 0) events.push(FocusEvent.FOCUS_OUT);
		if((flags & EventType.KEY_DOWN) 	> 0) events.push(KeyboardEvent.KEY_DOWN);
		if((flags & EventType.KEY_UP) 		> 0) events.push(KeyboardEvent.KEY_UP);
		
		return events;
	}
	
	/**
	 * Hace el trabajo sucio de registrar/quitar eventos.
	 * @see #addEventsListener()
	 * @see #removeEventsListener()
	 */
	private static function registerEvents(add : Boolean, target : InteractiveObject, callback : Function, flags : uint,
									buttonMode : Boolean, mouseChildren : Boolean,
									useCapture : Boolean, priority : int = 0, useWeakReference : Boolean = false) : void
	{
		if(target is Sprite)					target['buttonMode'] = buttonMode;
		if(target is DisplayObjectContainer)	target['mouseChildren'] = mouseChildren;
		target.mouseEnabled = true;
		
		var events : Array = extractEventsFromFlags(flags);
		
		for(var i:int=0; i<events.length; i++)
		{
			if(add)
				target.addEventListener(events[i], callback, useCapture, priority, useWeakReference);
			else
				target.removeEventListener(events[i], callback, useCapture);
		}
	}
}
}